using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;

namespace MatrixFramework.Shared.Application.Files
{
    /// <summary>
    /// 文件存储应用服务接口
    /// </summary>
    public interface IFileStorageAppService : IApplicationService
    {
        /// <summary>
        /// 上传文件
        /// </summary>
        Task<FileUploadResponseDto> UploadAsync(Stream fileStream, FileUploadRequestDto request);

        /// <summary>
        /// 上传文件（字节数组）
        /// </summary>
        Task<FileUploadResponseDto> UploadAsync(byte[] fileBytes, FileUploadRequestDto request);

        /// <summary>
        /// 批量上传文件
        /// </summary>
        Task<List<FileUploadResponseDto>> UploadAsync(Dictionary<string, Stream> files, FileUploadRequestDto request);

        /// <summary>
        /// 下载文件
        /// </summary>
        Task<(Stream FileStream, string FileName, string ContentType)> DownloadAsync(string fileId, FileDownloadRequestDto request = null);

        /// <summary>
        /// 获取文件信息
        /// </summary>
        Task<FileStorageDto> GetAsync(string fileId);

        /// <summary>
        /// 获取文件列表
        /// </summary>
        Task<PagedResultDto<FileStorageDto>> GetListAsync(FileSearchInputDto input);

        /// <summary>
        /// 删除文件
        /// </summary>
        Task DeleteAsync(string fileId);

        /// <summary>
        /// 批量删除文件
        /// </summary>
        Task<BulkOperationResultDto> BulkDeleteAsync(List<string> fileIds);

        /// <summary>
        /// 更新文件信息
        /// </summary>
        Task<FileStorageDto> UpdateAsync(string fileId, UpdateFileRequestDto request);

        /// <summary>
        /// 复制文件
        /// </summary>
        Task<FileStorageDto> CopyAsync(string fileId, string newFileName = null);

        /// <summary>
        /// 移动文件
        /// </summary>
        Task<FileStorageDto> MoveAsync(string fileId, string newCategory = null);

        /// <summary>
        /// 生成缩略图
        /// </summary>
        Task<string> GenerateThumbnailAsync(string fileId, string size = "200x200");

        /// <summary>
        /// 获取文件预览URL
        /// </summary>
        Task<string> GetPreviewUrlAsync(string fileId, int? width = null, int? height = null);

        /// <summary>
        /// 验证文件完整性
        /// </summary>
        Task<FileValidationResultDto> ValidateFileAsync(string fileId);

        /// <summary>
        /// 获取文件统计信息
        /// </summary>
        Task<FileStatisticsDto> GetStatisticsAsync(StatisticsFilterDto filter = null);

        /// <summary>
        /// 清理过期文件
        /// </summary>
        Task<FileCleanupResultDto> CleanupExpiredFilesAsync(int retentionDays = 30);

        /// <summary>
        /// 导出文件列表
        /// </summary>
        Task<byte[]> ExportAsync(FileSearchInputDto input, string format = "xlsx");

        /// <summary>
        /// 导入文件元数据
        /// </summary>
        Task<FileImportResultDto> ImportMetadataAsync(Stream metadataStream);

        /// <summary>
        /// 检测病毒
        /// </summary>
        Task<VirusScanResultDto> ScanForVirusAsync(string fileId);

        /// <summary>
        /// 获取文件访问日志
        /// </summary>
        Task<PagedResultDto<FileAccessLogDto>> GetAccessLogsAsync(string fileId, FileAccessLogInputDto input);
    }

    /// <summary>
    /// 更新文件请求DTO
    /// </summary>
    public class UpdateFileRequestDto
    {
        /// <summary>
        /// 文件名
        /// </summary>
        public string FileName { get; set; }

        /// <summary>
        /// 文件分类
        /// </summary>
        public string Category { get; set; }

        /// <summary>
        /// 文件描述
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 文件标签
        /// </summary>
        public List<string> Tags { get; set; } = new();

        /// <summary>
        /// 是否公开访问
        /// </summary>
        public bool? IsPublic { get; set; }

        /// <summary>
        /// 过期时间
        /// </summary>
        public DateTime? ExpirationTime { get; set; }

        /// <summary>
        /// 下载次数限制
        /// </summary>
        public int? DownloadLimit { get; set; }

        /// <summary>
        /// 自定义元数据
        /// </summary>
        public Dictionary<string, object> Metadata { get; set; } = new();
    }

    /// <summary>
    /// 分页结果DTO
    /// </summary>
    public class PagedResultDto<T>
    {
        /// <summary>
        /// 数据项
        /// </summary>
        public List<T> Items { get; set; } = new();

        /// <summary>
        /// 总数量
        /// </summary>
        public int TotalCount { get; set; }
    }

    /// <summary>
    /// 批量操作结果DTO
    /// </summary>
    public class BulkOperationResultDto
    {
        /// <summary>
        /// 成功数量
        /// </summary>
        public int SuccessCount { get; set; }

        /// <summary>
        /// 失败数量
        /// </summary>
        public int FailureCount { get; set; }

        /// <summary>
        /// 操作结果详情
        /// </summary>
        public List<BulkOperationItemDto> Results { get; set; } = new();
    }

    /// <summary>
    /// 批量操作项DTO
    /// </summary>
    public class BulkOperationItemDto
    {
        /// <summary>
        /// 项目ID
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// 是否成功
        /// </summary>
        public bool Success { get; set; }

        /// <summary>
        /// 错误信息
        /// </summary>
        public string ErrorMessage { get; set; }
    }

    /// <summary>
    /// 文件验证结果DTO
    /// </summary>
    public class FileValidationResultDto
    {
        /// <summary>
        /// 是否有效
        /// </summary>
        public bool IsValid { get; set; }

        /// <summary>
        /// 错误信息
        /// </summary>
        public List<string> Errors { get; set; } = new();

        /// <summary>
        /// 警告信息
        /// </summary>
        public List<string> Warnings { get; set; } = new();

        /// <summary>
        /// 验证详情
        /// </summary>
        public Dictionary<string, object> Details { get; set; } = new();
    }

    /// <summary>
    /// 统计过滤器DTO
    /// </summary>
    public class StatisticsFilterDto
    {
        /// <summary>
        /// 分类过滤
        /// </summary>
        public string Category { get; set; }

        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime? StartTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? EndTime { get; set; }

        /// <summary>
        /// 创建者ID
        /// </summary>
        public string CreatedById { get; set; }

        /// <summary>
        /// 是否包含已删除文件
        /// </summary>
        public bool IncludeDeleted { get; set; }
    }

    /// <summary>
    /// 文件清理结果DTO
    /// </summary>
    public class FileCleanupResultDto
    {
        /// <summary>
        /// 检查的文件数量
        /// </summary>
        public int CheckedFiles { get; set; }

        /// <summary>
        /// 删除的文件数量
        /// </summary>
        public int DeletedFiles { get; set; }

        /// <summary>
        /// 释放的存储空间（字节）
        /// </summary>
        public long FreedSpace { get; set; }

        /// <summary>
        /// 清理详情
        /// </summary>
        public List<CleanupItemDto> Details { get; set; } = new();
    }

    /// <summary>
    /// 清理项DTO
    /// </summary>
    public class CleanupItemDto
    {
        /// <summary>
        /// 文件ID
        /// </summary>
        public string FileId { get; set; }

        /// <summary>
        /// 文件名
        /// </summary>
        public string FileName { get; set; }

        /// <summary>
        /// 文件大小
        /// </summary>
        public long FileSize { get; set; }

        /// <summary>
        /// 删除原因
        /// </summary>
        public string Reason { get; set; }
    }

    /// <summary>
    /// 文件导入结果DTO
    /// </summary>
    public class FileImportResultDto
    {
        /// <summary>
        /// 成功数量
        /// </summary>
        public int SuccessCount { get; set; }

        /// <summary>
        /// 失败数量
        /// </summary>
        public int FailureCount { get; set; }

        /// <summary>
        /// 跳过数量
        /// </summary>
        public int SkippedCount { get; set; }

        /// <summary>
        /// 导入详情
        /// </summary>
        public List<FileImportItemDto> Details { get; set; } = new();
    }

    /// <summary>
    /// 文件导入项DTO
    /// </summary>
    public class FileImportItemDto
    {
        /// <summary>
        /// 文件ID
        /// </summary>
        public string FileId { get; set; }

        /// <summary>
        /// 文件名
        /// </summary>
        public string FileName { get; set; }

        /// <summary>
        /// 操作状态
        /// </summary>
        public string Status { get; set; }

        /// <summary>
        /// 错误信息
        /// </summary>
        public string ErrorMessage { get; set; }
    }

    /// <summary>
    /// 病毒扫描结果DTO
    /// </summary>
    public class VirusScanResultDto
    {
        /// <summary>
        /// 扫描ID
        /// </summary>
        public string ScanId { get; set; }

        /// <summary>
        /// 是否安全
        /// </summary>
        public bool IsSafe { get; set; }

        /// <summary>
        /// 检测到的威胁
        /// </summary>
        public List<VirusThreatDto> Threats { get; set; } = new();

        /// <summary>
        /// 扫描引擎信息
        /// </summary>
        public string ScanEngine { get; set; }

        /// <summary>
        /// 病毒定义版本
        /// </summary>
        public string VirusDefinitionsVersion { get; set; }

        /// <summary>
        /// 扫描时间
        /// </summary>
        public DateTime ScanTime { get; set; }

        /// <summary>
        /// 扫描耗时（毫秒）
        /// </summary>
        public long ScanDuration { get; set; }
    }

    /// <summary>
    /// 病毒威胁DTO
    /// </summary>
    public class VirusThreatDto
    {
        /// <summary>
        /// 威胁名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 威胁类型
        /// </summary>
        public string Type { get; set; }

        /// <summary>
        /// 严重程度
        /// </summary>
        public string Severity { get; set; }

        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 建议操作
        /// </summary>
        public string RecommendedAction { get; set; }
    }

    /// <summary>
    /// 文件访问日志DTO
    /// </summary>
    public class FileAccessLogDto
    {
        /// <summary>
        /// 日志ID
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// 文件ID
        /// </summary>
        public string FileId { get; set; }

        /// <summary>
        /// 用户ID
        /// </summary>
        public string UserId { get; set; }

        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 操作类型
        /// </summary>
        public string Operation { get; set; }

        /// <summary>
        /// IP地址
        /// </summary>
        public string IpAddress { get; set; }

        /// <summary>
        /// 用户代理
        /// </summary>
        public string UserAgent { get; set; }

        /// <summary>
        /// 操作时间
        /// </summary>
        public DateTime OperationTime { get; set; }

        /// <summary>
        /// 操作结果
        /// </summary>
        public bool Success { get; set; }

        /// <summary>
        /// 错误信息
        /// </summary>
        public string ErrorMessage { get; set; }

        /// <summary>
        /// 额外信息
        /// </summary>
        public Dictionary<string, object> AdditionalInfo { get; set; } = new();
    }

    /// <summary>
    /// 文件访问日志查询参数DTO
    /// </summary>
    public class FileAccessLogInputDto
    {
        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime? StartTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? EndTime { get; set; }

        /// <summary>
        /// 操作类型过滤
        /// </summary>
        public string Operation { get; set; }

        /// <summary>
        /// 用户ID过滤
        /// </summary>
        public string UserId { get; set; }

        /// <summary>
        /// IP地址过滤
        /// </summary>
        public string IpAddress { get; set; }

        /// <summary>
        /// 是否成功过滤
        /// </summary>
        public bool? Success { get; set; }

        /// <summary>
        /// 排序方式
        /// </summary>
        public string Sorting { get; set; }

        /// <summary>
        /// 跳过数量
        /// </summary>
        public int SkipCount { get; set; }

        /// <summary>
        /// 最大结果数量
        /// </summary>
        public int MaxResultCount { get; set; }
    }
}